package com.xzit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzit.entity.Channel;
import com.xzit.service.IChannelService;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 栏目表 前端控制器
 * </p>
 *
 * @author zouyu
 * @since 2025-08-05
 */
@Controller
@RequestMapping("/channel")

public class ChannelController {
    @Resource
    private IChannelService channelService;
    // 分页列表
    @GetMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       Model model) {
        Page<Channel> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Channel> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", 0);
        Page<Channel> channelPage = channelService.page(page, wrapper);

        model.addAttribute("page", channelPage);
        return "list";
    }

    // 添加页面
    @GetMapping("/add")
    public String add() {
        return "channel/add";
    }

    // 保存添加
    @PostMapping("/save")
    public String save(Channel channel) {
        channelService.save(channel);
        return "redirect:/channel/list";
    }

    // 编辑页面
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Integer id, Model model) {
        Channel channel = channelService.getById(id);
        model.addAttribute("channel", channel);
        return "channel/edit";
    }

    // 保存编辑
    @PostMapping("/update")
    public String update(Channel channel) {
        channelService.updateById(channel);
        return "redirect:/channel/list";
    }

    // 删除
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        channelService.removeById(id);
        return "redirect:/channel/list";
    }



}
